Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom exception handler #6710

Closed
wants to merge 17 commits into from

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Oct 18, 2022

Description
Supersedes #5675

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added enhancement PRs that improve existing functionalities 4.3 labels Oct 18, 2022
@kenjis kenjis requested a review from lonnieezell October 18, 2022 07:32
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big one, I will have to come back to it! Initially it looks really good. Please be sure to work with @lonnieezell for feature consistency and @paulbalandan if there's any collaboration to be had with the deprecations handler PR.

system/Debug/BaseExceptionHandler.php Outdated Show resolved Hide resolved
@kenjis kenjis requested a review from paulbalandan October 18, 2022 11:38
@lonnieezell
Copy link
Member

I'll be honest - this week I'm completely wrecked for time so I won't be able to really review until next week.

That said - the reason nothing happened with the original one was because it was a BC break and would have to wait until v5. Has the BC break portion been fixed with this PR?

@kenjis
Copy link
Member Author

kenjis commented Oct 18, 2022

No problem. The original PR was created many months ago.

I have tried to keep this PR free of BC breaks.

@kenjis kenjis force-pushed the feat-ExceptionHandler branch from b22c181 to 2017313 Compare October 23, 2022 07:32
@kenjis
Copy link
Member Author

kenjis commented Oct 23, 2022

Rebased.

@kenjis kenjis force-pushed the feat-ExceptionHandler branch 2 times, most recently from af3f99e to 9db24aa Compare October 24, 2022 23:37
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at docs and tests yet, but this is looking great! I added some comments but two broad thoughts:

  1. It seems like the new classes are independent of the existing ones, and you use the current handle as a mediator to call them when available. I like the freedom that gives us - but then you kept ExceptionHandler compatible with the current class. It don't understand the need for this backwards-compatibility, unless I am missing something?
  2. I know we've been burned by class-interface-abstraction, but enforcing our BaseExceptionHandler for a single handle() method feels unduly restrictive. I would like to see an interface (just that method) used as the return from the Config file and the abstract class becomes optional.

system/Debug/BaseExceptionHandler.php Show resolved Hide resolved
*
* @return bool|string
*/
protected static function highlightFile(string $file, int $lineNumber, int $lines = 15)
Copy link
Member

@MGatner MGatner Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the existing handler doesn't actually extend this base, can we go ahead and type this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

@@ -122,6 +125,23 @@ public function exceptionHandler(Throwable $exception)
]);
}

// For upgraded users.
if (! method_exists($this->config, 'handler')) {
$this->defaultExceptionHandler($exception, $statusCode, $exitCode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this use ExceptionHandler? Is it not backwards-compatible behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.
It should use ExceptionHandler.
When using ExceptionHandler, if the behavior changes it is a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

system/Debug/ExceptionHandler.php Outdated Show resolved Hide resolved
/**
* Determines the correct way to display the error.
*
* @return void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like a big loss not having this in the abstraction. Any reason not to do that?

@kenjis kenjis force-pushed the feat-ExceptionHandler branch from 9db24aa to b93407c Compare October 26, 2022 09:45
@kenjis kenjis force-pushed the feat-ExceptionHandler branch from b93407c to 9d5d44f Compare October 27, 2022 08:21
@kenjis
Copy link
Member Author

kenjis commented Oct 27, 2022

No. If a dev overrides the following methods, this PR breaks the extended Exceptions:

  • Exceptions::render()
  • Exceptions::determineView()
  • Exceptions::collectVars()
  • Exceptions::maskSensitiveData()

@kenjis kenjis added the breaking change Pull requests that may break existing functionalities label Oct 27, 2022
@kenjis
Copy link
Member Author

kenjis commented Oct 27, 2022

Reverting 9d5d44f and a dev does not update the Config class,
then there is no BC break even if the dev extends Exceptions.

@MGatner
Copy link
Member

MGatner commented Oct 27, 2022

I'm confused; I think I missed something. ExceptionHandler and BaseExceptionHandler are new classes, and Exceptions is not being changed to extend either. So why do the new classes need to be backwards-compatible with Exceptions?

@MGatner
Copy link
Member

MGatner commented Oct 27, 2022

No. If a dev overrides the following methods, this PR breaks the extended Exceptions

If we want to support extended classes just check if (static::class === self::class) before passing off to the handler.

@kenjis
Copy link
Member Author

kenjis commented Oct 27, 2022

I'm confused; I think I missed something. ExceptionHandler and BaseExceptionHandler are new classes, and Exceptions is not being changed to extend either. So why do the new classes need to be backwards-compatible with Exceptions?

The new classes do not need to be backwards-compatible with Exceptions.

A dev can replace Exceptions with their custom extended class now, and if the dev overrides one of some methods,
this PR breaks the custom Exceptions.

If we want to support extended classes just check if (static::class === self::class) before passing off to the handler.

It may be Okay with that check, as a dev would not need to use their own ExceptionHandler and replace Exceptions at the same time.
Alternatively, it can also be determined by setting it to opt-in in the Config.

Copy link
Member

@lonnieezell lonnieezell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me. I just found one edge case that we don't handle.

$viewFile = null;
if (is_file($path . $view)) {
$viewFile = $path . $view;
} elseif (is_file($altPath . $altView)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we need to handle if neither file exists?

@kenjis kenjis added 4.4 and removed 4.3 labels Oct 28, 2022
@MGatner
Copy link
Member

MGatner commented Oct 28, 2022

The new classes do not need to be backwards-compatible with Exceptions.

Since this is the case please add explicit types to all the new class methods.

I am also still keen on adding an interface - nothing else in the base class is mandatory so we shouldn't restrict users to extending it.

@kenjis kenjis deleted the branch codeigniter4:4.3 January 10, 2023 06:36
@kenjis kenjis closed this Jan 10, 2023
@kenjis kenjis mentioned this pull request Jan 11, 2023
5 tasks
@kenjis kenjis deleted the feat-ExceptionHandler branch February 8, 2023 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.4 breaking change Pull requests that may break existing functionalities enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants